/************************************************************************/
/* particleEmitter.h                                                    */
/************************************************************************/

#ifndef         __PARTICLEEMITTER_H__
#define         __PARTICLEEMITTER_H__


#if _MSC_VER > 1000
#pragma once
#endif 


class CParticleEmitter
{
    public:
    CParticleEmitter():m_fTicksPerFrame(160.0f),
        m_fFrameSpeed(30.0f),
        m_fLastFrame(30.0f),
        m_fLifeTime(4800.0f),
        m_bTail(false),
        m_fMinEmitRate(800.0f),
        m_fMaxEmitRate(800.0f),
        m_vSpawnDir1(0.0f, 0.0f, 0.0f),
        m_vSpawnDir2(0.0f, 0.0f, 0.0f),
        m_vPos1(0.0f, 0.0f, 0.0f),
        m_vPos2(0.0f, 0.0f, 0.0f),
        m_pd3dDevice(0),
        m_texParticle(0),
        m_fNumNewPartsExcess(0.0f),
		m_fGravity(0.0f),
		m_bSphere(FALSE),
		m_fRadius(1.0f)
    {
        D3DXMatrixIdentity( &m_matIdentity );
    };	
    ~CParticleEmitter(){};
    
    private:    
    void AddColor(const float& fTickNum, const D3DXCOLOR& color)
    {
        m_EventList.AddToList(fTickNum, color);
    }
    D3DXCOLOR GetColor(const float& fTickNum)
    {
        return m_EventList.GetColor(fTickNum);
    }
    D3DXVECTOR3 GetScale(const float& fTickNum)
    {
        return m_EventList.GetScale(fTickNum);
    }
    void AddScale(const float& fTickNum, const D3DXVECTOR3& vScale)
    {
        m_EventList.AddToList(fTickNum, vScale);
    }
    void SetFrameProps(float fFrameSpeed,
        float fTicksPerFrame, float fLastFrame);

    void SetRenderState(void);
    void RenewParts(CPART_DATA& partData, const float& fElapsed);    
    void GenNewParts(CPART_DATA& partData, const float& fElapsed);    
    void CalcPartTransform(CPART_DATA& partData, const int& nNumNewParts);
    void DrawParticles(CPART_DATA& partData);
    
    public:    
    void SetMinEmitRate(const float data) { m_fMinEmitRate = data; }
    void SetMaxEmitRate(const float data) { m_fMaxEmitRate = data; }
    void SetLifeTime(float data){m_fLifeTime = data;}
    void SetEndTime(float data){m_fEndTime = data;}
    void SetSize(float data){m_fSize = data;} 

    public:
    float GetMinEmitRate(void) const { return(m_fMinEmitRate); }	
    float GetMaxEmitRate(void) const { return(m_fMaxEmitRate); }
    D3DXVECTOR3 GetSpawnDir1(void) const { return(m_vSpawnDir1); }
    D3DXVECTOR3 GetSpawnDir2(void) const { return(m_vSpawnDir2); }

    void SetSpawnDir1(const D3DXVECTOR3 &data) { m_vSpawnDir1 = data; }
    void SetSpawnDir2(const D3DXVECTOR3 &data) { m_vSpawnDir2 = data; }
    void SetEmitRate(float fMinEmitRate, float fMaxEmitRate)
    {
        m_fMaxEmitRate = fMaxEmitRate;
        m_fMinEmitRate = fMinEmitRate;
    }
    void SetSpawnDir(D3DXVECTOR3 vSpawnDir1, D3DXVECTOR3 vSpawnDir2)
    {
        m_vSpawnDir1 = vSpawnDir1;
        m_vSpawnDir2 = vSpawnDir2;
    }
    void SetPos(D3DXVECTOR3 vPos1, D3DXVECTOR3 vPos2)
    {
        m_vPos1 = vPos1;
        m_vPos2 = vPos2;
    }
    HRESULT LoadObjects();	

    public:
    bool Update(CPART_DATA& partData, const float& fElpased);
    void Render(CPART_DATA& partData);
    
    HRESULT RestoreDeviceObjects(LPDIRECT3DDEVICE9 pDev,
        VertexBufferItem* pVB);
    void InvalidateDeviceObjects();
    void DecodeEventScript(const char* strEventScript);

    private:
    char m_szTextureName[128];
    float m_fSize;   
    float m_fTicksPerFrame;
    float m_fFrameSpeed;
    float m_fLastFrame;	
    float m_fLifeTime;
    float m_fEndTime;	
    bool m_bTail;    	
	float m_fGravity;
	bool m_bSphere;
	float m_fRadius;
    CEventList m_EventList;

    private:
    D3DXMATRIX m_matIdentity;
    float m_fMinEmitRate; 
    float m_fMaxEmitRate; 

    D3DXVECTOR3 m_vSpawnDir1;
    D3DXVECTOR3 m_vSpawnDir2;
    D3DXVECTOR3 m_vPos1;
    D3DXVECTOR3 m_vPos2;
    float m_fNumNewPartsExcess;

    LPDIRECT3DDEVICE9 m_pd3dDevice;
    VertexBufferItem* m_vbParticles;
//    LPDIRECT3DTEXTURE9 m_texParticle; 
    TextureItem* m_texParticle; 
};

#endif
